(function() {
  var format, format_num, length, unit;

  format = function(size) {
    if (size >= 1024 * 1024 * 1024) {
      return (size / (1024 * 1024 * 1024)).toFixed(2) + ' GB';
    } else if (1024 * 1024 * 1024 > size && size >= 1024 * 1024) {
      return (size / (1024 * 1024)).toFixed(2) + ' MB';
    } else {
      return (size / 1024).toFixed(2) + ' KB';
    }
  };

  length = 0;

  unit = '';

  format_num = function(size) {
    if (size >= 1024 * 1024 * 1024) {
      length = 1024 * 1024 * 1024;
      unit = 'GB';
      return (size / (1024 * 1024 * 1024)).toFixed(2);
    } else if (1024 * 1024 * 1024 > size && size >= 1024 * 1024) {
      length = 1024 * 1024;
      unit = 'MB';
      return (size / (1024 * 1024)).toFixed(2);
    } else {
      length = 1024;
      unit = 'KB';
      return (size / 1024).toFixed(2);
    }
  };

  $(function() {
    var all_rx, all_tx, all_value, format_rx, format_tx, index, item, max, network_stats, network_stats_chart, option, _i, _j, _k, _len, _len1, _len2;
    network_stats = JSON.parse(window.network_stats);
    network_stats_chart = echarts.init(document.getElementById('network_stats'));
    all_rx = 0;
    for (_i = 0, _len = network_stats.length; _i < _len; _i++) {
      item = network_stats[_i];
      all_rx += item.rx;
    }
    format_rx = format(all_rx);
    all_tx = 0;
    for (_j = 0, _len1 = network_stats.length; _j < _len1; _j++) {
      item = network_stats[_j];
      all_tx += item.tx;
    }
    format_tx = format(all_tx);
    option = {
      title: {
        text: '网络使用情况',
        subtext: '已接收：' + format_rx + '，已发送：' + format_tx,
        subtextStyle: {
          color: 'blue'
        },
        x: 'center',
        padding: [0, 0]
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        },
        formatter: function(params) {
          var rx, tx;
          if (!network_stats[params[0].dataIndex]) {
            return '未知';
          } else {
            if (network_stats[params[0].dataIndex].operstate === 'up') {
              rx = format(params[0].value);
              tx = format(params[1].value);
              return '接口名称：' + params[0].name + '<br/>' + params[0].seriesName + '：' + rx + '<br/>' + params[1].seriesName + '：' + tx;
            } else if (network_stats[params[0].dataIndex].operstate === 'down') {
              return '接口已禁用';
            }
          }
        }
      },
      legend: {
        orient: 'vertical',
        x: 'left',
        data: ['已接收', '已发送']
      },
      grid: {
        left: '3%',
        right: '20%',
        bottom: '3%',
        top: '25%',
        containLabel: true
      },
      xAxis: [
        {
          type: 'value',
          name: '(B)',
          axisLabel: {
            formatter: function(value, index) {
              var sub_label;
              sub_label = (value / length).toFixed(2);
              return sub_label;
            }
          }
        }
      ],
      yAxis: [
        {
          type: 'category',
          axisTick: {
            show: false
          },
          data: [],
          name: '网络接口'
        }
      ],
      series: [
        {
          name: '已接收',
          type: 'bar',
          barCategoryGap: '30%',
          barWidth: '20px',
          label: {
            normal: {
              show: true,
              position: 'right',
              formatter: function(params) {
                var value;
                value = format(params.value);
                return value;
              }
            }
          },
          itemStyle: {
            normal: {
              color: '#00A600'
            }
          },
          data: []
        }, {
          name: '已发送',
          type: 'bar',
          barCategoryGap: '30%',
          barWidth: '20px',
          label: {
            normal: {
              show: true,
              position: 'right',
              formatter: function(params) {
                var value;
                value = format(params.value);
                return value;
              }
            }
          },
          itemStyle: {
            normal: {
              color: '#AE0000'
            }
          },
          data: []
        }
      ]
    };
    all_value = [];
    for (index = _k = 0, _len2 = network_stats.length; _k < _len2; index = ++_k) {
      item = network_stats[index];
      option.yAxis[0].data[index] = item.iface;
      option.series[0].data[index] = item.rx;
      option.series[1].data[index] = item.tx;
      all_value.push(item.rx);
      all_value.push(item.tx);
    }
    max = Math.max.apply(Math, all_value);
    format_num(max);
    option.xAxis[0].name = unit;
    return network_stats_chart.setOption(option);
  });

}).call(this);
